#include <Arduino.h>
#include <WiFi.h>        // 引入WiFi库文件，用于连接WiFi网络
#include "WiFiAirlink.h" // 引入WiFi库文件，用于连接WiFi网络
TaskHandle_t CoreTask1;  // 声明CoreTask1任务句柄，用于在核心1上运行任务
TaskHandle_t CoreTask2;  // 声明CoreTask2任务句柄，用于在核心1上运行任务
TaskHandle_t CoreTask3;  // 声明CoreTask3任务句柄，用于在核心1上运行任务
char DEV_ChipID[16];     // 设备ID
void airlink();          // 声明airlink函数，用于连接MQTT服务器
uint8_t receivedData[16];
uint8_t key[2] = {0x00, 0x00};
bool control = false;
uint8_t Equipment_status_machine[10] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x06, 0x02, 0x00, 0x01}; // 设备状态机，用于控制设备状态的数组
uint8_t Relay_1_state_machine[10] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x06, 0x02, 0x00, 0x00};
uint8_t Relay_2_state_machine[10] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x06, 0x02, 0x00, 0x00};
uint8_t Relay_state_machine[12] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x06, 0x04, 0x00, 0x00, 0x00, 0x00};
uint8_t Relay_1_control_machine[10] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x02, 0x00, 0x00};
uint8_t Relay_2_control_machine[10] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x02, 0x00, 0x00};
uint8_t Relay_control_machine[12] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00};
uint8_t error[] = {0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600, SERIAL_8N1, 18, 19); // 初始化串口1，设置波特率为9600, 数据位8, 无校验位, 停止位1, RX引脚为GPIO18, TX引脚为GPIO19
  Serial1.setTimeout(10);  // 关键！修改readBytes的超时时间（原默认1000ms，改为10ms减少阻塞）
  pinMode(5,OUTPUT);
   pinMode(4,OUTPUT);
  uint32_t chipId = 0;
  for (int i = 0; i < 17; i = i + 8)
  {
    chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
  }
  snprintf(DEV_ChipID, 16, "SLKZ-%06X", chipId);
}

void loop()
{
  if (Serial1.available() > 0)
  {
    Serial1.readBytes(receivedData, 16);
    switch (receivedData[0])
    {
    case 0x00:
      break;
    case 0x03:
      switch (receivedData[3])
      {
      case 0x01:
        if (receivedData[5])
        {
          digitalWrite(5, HIGH);
          key[0] = 0x01;
          Relay_1_control_machine[9] = key[0];
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
        }
        else
        {
          digitalWrite(5, LOW);
          key[0] = 0x00;
          Relay_1_control_machine[9] = key[0];
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
        }
        break;
      case 0x02:
        if (receivedData[5])
        {
          digitalWrite(4, HIGH);
          key[1] = 0x01;
          Relay_2_control_machine[9] = key[1];
          Serial1.write(Relay_2_control_machine, sizeof(Relay_2_control_machine));
        }
        else
        {
          digitalWrite(4, LOW);
          key[1] = 0x00;
          Relay_2_control_machine[9] = key[1];
          Serial1.write(Relay_2_control_machine, sizeof(Relay_2_control_machine));
        }
        break;
      case 0x03:
        if (receivedData[5])
        {
          digitalWrite(5, HIGH);
          key[0] = 0x01;
        }
        else
        {
          digitalWrite(5, LOW);
          key[0] = 0x00;
        }
        if (receivedData[7])
        {
          digitalWrite(4, HIGH);
          key[1] = 0x01;
        }
        else
        {
          digitalWrite(4, LOW);
          key[1] = 0x00;
        }
        Relay_control_machine[9] = key[0];
        Relay_control_machine[11] = key[1];
        Serial1.write(Relay_control_machine, sizeof(Relay_state_machine));
        break;
      case 0x07:
        airlink();
        esp_restart();
        break;
      }
      break;
    case 0x06:
      switch (receivedData[3])
      {
      case 0x00:
        Serial1.write(Equipment_status_machine, sizeof(Equipment_status_machine));
        break;
      case 0x03:
        Relay_1_state_machine[9] = key[0];
        Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        break;
      case 0x04:
        Relay_2_state_machine[9] = key[1];
        Serial1.write(Relay_2_state_machine, sizeof(Relay_2_state_machine));
        break;
      case 0x06:
        Relay_state_machine[9] = key[0];
        Relay_state_machine[11] = key[1];
        Serial1.write(Relay_state_machine, sizeof(Relay_state_machine));
        break;
      }
      break;
    }
  }
  for (int i = 0; i < 16; i++)
  {
    receivedData[i] = 0x00;
  }
}
void airlink()
{ // 新配网程序
  WiFiAirlink Airlink;
  Airlink.setDebugOutput(false);
  Airlink.setMinimumSignalQuality(30); // 只列出信号质量超过30%的SSID
  Airlink.setConfigPortalTimeout(60);  // 60秒不配网操作则退出
  Airlink.setConnectTimeout(30);
  Airlink.startConfigPortal(DEV_ChipID); // 强制开启配网AP
  vTaskDelay(100);
}